package itheima.com.tabhostdemo;

import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TabHost;

public class MainActivity extends AppCompatActivity {
    private FragmentTabHost tabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //引用FragmentTabHost
        tabHost= (FragmentTabHost) findViewById(R.id.tabhost);
        /** 1. 初始化FragmentTabHost
         * context :上下文
         * FragmentManager ：片段管理器
         * containerId:容纳片段的id
         */
        tabHost.setup(this,getSupportFragmentManager(),R.id.fl_content);



/*
        //2.1 准备TabSpec
        TabHost.TabSpec tabSpec=tabHost.newTabSpec("news");
        tabSpec.setIndicator("综合"); //参数可以是字符串或者View视图对象
        //2.2 准备片段
        //2.3 准备向片段传递的参数
        Bundle agrs=getBundle("综合的内容");
        *//** 2. 向FragmentTabHost添加标签
         *  TabSpec :具体的标签类
         *  Class：标签关联的片段的class
         *  bundles ：向片段传递的参数
         *//*
        tabHost.addTab(tabSpec,DefaultFragment.class,agrs);

        //添加动弹标签
        TabHost.TabSpec tabSpec2=tabHost.newTabSpec("tweets");
        tabSpec2.setIndicator("动弹");
        tabHost.addTab(tabSpec2,DefaultFragment.class,getBundle("动弹的内容"));*/


        //3. 通过枚举类添加底部标签
        addTab();

    }

    /**
     * 添加底部标签
     *
     */
    private void addTab() {
        MainTab[] values = MainTab.values();
        //遍历所有枚举对象
        for (int i = 0; i <values.length ; i++) {
            //取得当前枚举对象
            MainTab value = values[i];
            TabHost.TabSpec spec=tabHost.newTabSpec(value.getTag());
            //设置标签的指示器
            spec.setIndicator(value.getTitle());
            //向标签容器添加标签
            tabHost.addTab(spec,value.getClz(),getBundle(value.getTitle()));
        }
    }

    //取得bundle对象
    private Bundle getBundle(String info) {
        Bundle bundle=new Bundle( );
        bundle.putString("key",info);
        return  bundle;
    }
}
